﻿using Engine;
using Game.NetWork;
using Game.Server.PlayerEvent;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Game.Server.Plugins
{
    public class InventoryRecordPlugin : ServerPlugin, IPlayerInventoryHandle
    {
        public override int Version => 10000;

        public override string Name => "背包方块记录";

        public byte FirstLevel => 0;

        public override void Initialize()
        {
            PlayerInventoryEventManager.AddObject(this);
        }

        public override void Save()
        {
        }

        public override void Load()
        {

        }
        public bool PlayerDrapDrop(ComponentPlayer componentPlayer, Vector3 worldPos, InventoryDragData inventoryDragData, int count)
        {
            int valueID = inventoryDragData.Inventory.GetSlotValue(inventoryDragData.SlotIndex);

            Point3 point = new Point3(componentPlayer.ComponentBody.Position);

            switch (valueID)
            {
                case 40:
                    Log.Information($"[社区ID{componentPlayer.PlayerData.Client.CommunityAccountId}]{componentPlayer.PlayerData.Name} 扔掉铁锭，数量 {count} , 玩家位置 {point.X},{point.Y},{point.Z}");
                    break;
                case 42:
                    Log.Information($"[社区ID{componentPlayer.PlayerData.Client.CommunityAccountId}]{componentPlayer.PlayerData.Name} 扔掉铜锭，数量 {count} , 玩家位置 {point.X},{point.Y},{point.Z}");
                    break;
                case 111:
                    Log.Information($"[社区ID{componentPlayer.PlayerData.Client.CommunityAccountId}]{componentPlayer.PlayerData.Name} 扔掉钻石，数量 {count} , 玩家位置 {point.X},{point.Y},{point.Z}");
                    break;
                case 126:
                    Log.Information($"[社区ID{componentPlayer.PlayerData.Client.CommunityAccountId}]{componentPlayer.PlayerData.Name} 扔掉钻石块，数量 {count} , 玩家位置 {point.X},{point.Y},{point.Z}");
                    break;
            }

            var gameMode = componentPlayer.Project.FindSubsystem<SubsystemGameInfo>().WorldSettings.GameMode;
            if (gameMode != GameMode.Creative)
            {
                if (inventoryDragData.Inventory is ComponentCreativeInventory)
                {
                    Log.Information($"[社区ID{componentPlayer.PlayerData.Client.CommunityAccountId}]{componentPlayer.PlayerData.Name} 尝试在生存模式使用创造背包扔掉东西！！！\n IP:{componentPlayer.PlayerData.Client.Peer.EndPoint.Address.ToString()}");
                    CommonLib.Net.RemoveClient(componentPlayer.PlayerData.Client, "服务器断开连接");
                    return false;
                }
            }

            return true;
        }

        public bool PlayerDrop(ComponentPlayer componentPlayer)
        {
            return true;
        }

        public bool PlayerHandleDragDrop(ComponentPlayer componentPlayer, IInventory sourceInventory, int sourceSlotIndex, DragMode dragMode, IInventory targetInventory, int targetSlotIndex, bool processingOnly)
        {
            var gameMode = componentPlayer.Project.FindSubsystem<SubsystemGameInfo>().WorldSettings.GameMode;
            if (gameMode != GameMode.Creative)
            {
                if (sourceInventory is ComponentCreativeInventory || targetInventory is ComponentCreativeInventory)
                {
                    Log.Information($"[社区ID{componentPlayer.PlayerData.Client.CommunityAccountId}]{componentPlayer.PlayerData.Name} 尝试再生存模式使用创造背包移动东西！！！\n IP:{componentPlayer.PlayerData.Client.Peer.EndPoint.Address.ToString()}");
                    CommonLib.Net.RemoveClient(componentPlayer.PlayerData.Client, "服务器断开连接");
                    return false;
                }
            }

            int valueID = sourceInventory.GetSlotValue(sourceSlotIndex);
            int count = sourceInventory.GetSlotCount(sourceSlotIndex);

            Point3 point = new Point3(componentPlayer.ComponentBody.Position);

            switch (valueID)
            {
                case 40:
                    Log.Information($"[社区ID{componentPlayer.PlayerData.Client.CommunityAccountId}]{componentPlayer.PlayerData.Name} 移动铁锭，数量 {count} , 玩家位置 {point.X},{point.Y},{point.Z}");
                    break;
                case 42:
                    Log.Information($"[社区ID{componentPlayer.PlayerData.Client.CommunityAccountId}]{componentPlayer.PlayerData.Name} 移动铜锭，数量 {count} , 玩家位置 {point.X},{point.Y},{point.Z}");
                    break;
                case 111:
                    Log.Information($"[社区ID{componentPlayer.PlayerData.Client.CommunityAccountId}]{componentPlayer.PlayerData.Name} 移动钻石，数量 {count} , 玩家位置 {point.X},{point.Y},{point.Z}");
                    break;
                case 126:
                    Log.Information($"[社区ID{componentPlayer.PlayerData.Client.CommunityAccountId}]{componentPlayer.PlayerData.Name} 移动钻石块，数量 {count} , 玩家位置 {point.X},{point.Y},{point.Z}");
                    break;
            }
            return true;
        }

        public bool PlayerHandleMoveItem(ComponentPlayer componentPlayer, IInventory sourceInventory, int sourceSlotIndex, IInventory targetInventory, int targetSlotIndex, int count)
        {
            var gameMode = componentPlayer.Project.FindSubsystem<SubsystemGameInfo>().WorldSettings.GameMode;
            if (gameMode != GameMode.Creative)
            {
                if (sourceInventory is ComponentCreativeInventory || targetInventory is ComponentCreativeInventory)
                {
                    Log.Information($"[社区ID{componentPlayer.PlayerData.Client.CommunityAccountId}]{componentPlayer.PlayerData.Name} 尝试再生存模式使用创造背包移动东西！！！\n IP:{componentPlayer.PlayerData.Client.Peer.EndPoint.Address.ToString()}");
                    CommonLib.Net.RemoveClient(componentPlayer.PlayerData.Client, "服务器断开连接");
                    return false;
                }
            }

            int valueID = sourceInventory.GetSlotValue(sourceSlotIndex);
            switch (valueID)
            {
                case 111:
                    Log.Information($"[社区ID{componentPlayer.PlayerData.Client.CommunityAccountId}]{componentPlayer.PlayerData.Name} 处理移动钻石，数量 {count} 背包:{sourceInventory.Id} 到 {targetInventory.Id}");
                    break;
                case 126:
                    Log.Information($"[社区ID{componentPlayer.PlayerData.Client.CommunityAccountId}]{componentPlayer.PlayerData.Name} 处理移动钻石块，数量 {count} 背包:{sourceInventory.Id} 到 {targetInventory.Id}");
                    break;
            }

            return true;
        }
    }
}